package com.muyu.danmo.service.impl;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoInformationBasic;
import com.muyu.danmo.domain.DanmoNews;
import com.muyu.danmo.domain.DanmoNewsPhone;
import com.muyu.danmo.mapper.DanmoInformationBarMapper;
import com.muyu.danmo.mapper.DanmoNewsMapper;
import com.muyu.danmo.service.IDanmoNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 时政要闻Service业务层处理
 *
 * @author yangle
 * @date 2024-09-19
 */
@Service
public class DanmoNewsServiceImpl implements IDanmoNewsService
{
    @Autowired
    private DanmoNewsMapper danmoNewsMapper;

    @Autowired
    private DanmoInformationBarMapper danmoInformationBasicMapper;
    /**
     * 查询时政要闻
     *
     * @param newsId 时政要闻主键
     * @return 时政要闻
     */
    @Override
    public Result selectDanmoNewsByNewsId(Long newsId)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        DanmoNews danmoNews = danmoNewsMapper.selectDanmoNewsByNewsId(newsId);
        if (loginUser == null) {
            return Result.success(danmoNews);
        }

        // 当 loginUser 不为 null 时执行以下逻辑
        danmoNews.setViews(danmoNews.getViews() + 1);
        danmoNewsMapper.updateDanmoNews(danmoNews);

        DanmoInformationBasic danmoInformationBasic = new DanmoInformationBasic();
        danmoInformationBasic.setUserId(danmoNews.getUserId());
        DanmoInformationBasic danmoInformationBasic1 = danmoInformationBasicMapper.selectByUserId(danmoInformationBasic);
        danmoNews.setDanmoInformationBasic(danmoInformationBasic1);

        return Result.success(danmoNews);

    }

    /**
     * 查询时政要闻列表
     *
     * @param danmoNews 时政要闻
     * @return 时政要闻
     */
    @Override
    public List<DanmoNews> selectDanmoNewsList(DanmoNews danmoNews)
    {
        return danmoNewsMapper.selectDanmoNewsList(danmoNews);
    }

    /**
     * 新增时政要闻
     *
     * @param danmoNews 时政要闻
     * @return 结果
     */
    @Transactional
    @Override
    public Result<Integer> insertDanmoNews(DanmoNews danmoNews)
    {

        danmoNews.setCreateTime(DateUtils.getNowDate());
        danmoNews.setViews(0L);
        int rows = danmoNewsMapper.insertDanmoNews(danmoNews);
        insertDanmoNewsPhone(danmoNews);
        return Result.success(rows, "新增成功");
    }

    /**
     * 修改时政要闻
     *
     * @param danmoNews 时政要闻
     * @return 结果
     */
    @Transactional
    @Override
    public Result<Integer> updateDanmoNews(DanmoNews danmoNews)
    {

            danmoNews.setUpdateTime(DateUtils.getNowDate());
            danmoNewsMapper.deleteDanmoNewsPhoneByNewsId(danmoNews.getNewsId());
            insertDanmoNewsPhone(danmoNews);
            int i = danmoNewsMapper.updateDanmoNews(danmoNews);
            return Result.success(i, "修改成功");


    }

    /**
     * 批量删除时政要闻
     *
     * @param newsIds 需要删除的时政要闻主键
     * @return 结果
     */
    @Transactional
    @Override
    public Result<Integer> deleteDanmoNewsByNewsIds(Long[] newsIds)
    {
        danmoNewsMapper.deleteDanmoNewsPhoneByNewsIds(newsIds);
        int i = danmoNewsMapper.deleteDanmoNewsByNewsIds(newsIds);
         return  Result.success(i, "删除成功");
    }

    /**
     * 删除时政要闻信息
     *
     * @param newsId 时政要闻主键
     * @return 结果
     */
    @Transactional
    @Override
    public Result<Integer> deleteDanmoNewsByNewsId(Long newsId)
    {
        danmoNewsMapper.deleteDanmoNewsPhoneByNewsId(newsId);
        int i = danmoNewsMapper.deleteDanmoNewsByNewsId(newsId);
        return Result.success(i, "删除成功");
    }

    /**
     * 新增新闻图片信息
     *
     * @param danmoNews 时政要闻对象
     */
    public void insertDanmoNewsPhone(DanmoNews danmoNews)
    {
        List<DanmoNewsPhone> danmoNewsPhoneList = danmoNews.getDanmoNewsPhoneList();
        Long newsId = danmoNews.getNewsId();
        if (StringUtils.isNotNull(danmoNewsPhoneList))
        {
            List<DanmoNewsPhone> list = new ArrayList<DanmoNewsPhone>();
            for (DanmoNewsPhone danmoNewsPhone : danmoNewsPhoneList)
            {
                danmoNewsPhone.setNewsId(newsId);
                list.add(danmoNewsPhone);
            }
            if (list.size() > 0)
            {
                danmoNewsMapper.batchDanmoNewsPhone(list);
            }
        }
    }
}
